//
//  WrongWordView.m
//  HotWord
//
//  Created by Rainbow on 9/2/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "WrongWordView.h"
#import "PersistenceData.h"
#import "WrongWordCell.h"
#import "CacheData.h"

@implementation WrongWordView
@synthesize wrongWordTableView;
@synthesize noContentLabel;

- (void)awakeFromNib
{
    wrongWords = [[NSMutableArray alloc] initWithArray:[[PersistenceData sharedData] getWrongWords]];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([wrongWords count] == 0) {
        noContentLabel.hidden = NO;
    }
    else
    {
        noContentLabel.hidden = YES;
    }
    return [wrongWords count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{        
    static NSString *CellIdentifier = @"WrongWordCell";
    
    WrongWordCell *cell = (WrongWordCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"WrongWordCell" owner:self options:nil] objectAtIndex:0];
        cell.myDelegate = self;
    }
    
    cell.cellIndex = indexPath.row;
    [cell setWrongWord:[wrongWords objectAtIndex:indexPath.row]];
    // Configure the cell...
    
    return cell;
}

//-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
//{
//    return YES;
//
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//    return 60.0f;
//
//}
//-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//        
//        [[CacheData sharedData] removeWrongWord:[wrongWords objectAtIndex:indexPath.row]];
////        
//        [[PersistenceData sharedData]syncWrongWords:[CacheData sharedData].wrongWords];
////        
//
////        
//
//    [self.wrongWordTableView setEditing:NO animated:YES];
//
//    
//       [self.wrongWordTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];
//        
//        [self.wrongWordTableView reloadData];
//      
//    }





//- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
//    return YES;
//}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)WrongWordCellDidRemove:(WrongWordCell *)wordCell
{
    
  
    [wrongWords removeObject:wordCell.word];
    // NSLog(@"%d",wordCell.cellIndex);
    [wrongWordTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:wordCell.cellIndex inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
  
    [[CacheData sharedData] removeWrongWord:wordCell.word];
    [wrongWordTableView reloadData];
    
}




- (void)dealloc
{
    [wrongWordTableView release];
    [wrongWords release];
    [noContentLabel release];
    [super dealloc];
}

@end
